package wili.insertCode;

import java.io.File;
import java.util.Properties;
import java.util.function.BiFunction;
import java.util.function.Predicate;

import wili.c.Ca;
import wili.c.File9a;
import wili.c.Misc9a;
import wili.c.Str9a;
import wili.misc.HourTimes;

/**
 * @author weila 2022年6月23日
 */
public class InsertTimeCostMethod_main extends InsertCode0 {
	public InsertTimeCostMethod_main() {
		super();
	}

	public boolean updateMethodPart(MethodPart funPart, String action) {
		if ("456".isEmpty())
			return true;
		if (funPart.isConstructor())
			return false;//can't rename constructor.
		MethodPart newPart = funPart.clone();
		newPart.setBody(new BracketPart("{}"));
		String methodSt = funPart.headSt;
		String oldName = funPart.name;
		{
			if (funPart.name.equals("load"))
				Coms2.pause();
			StringBuilder sb = new StringBuilder();
			String dent = "\n\t", dent2 = dent + "\t", dent3 = dent2 + "\t";
			sb.append(dent + funPart.headSt + "{");
			sb.append(dent2 + "try{");
			funPart.rename(funPart.name + "_" + Coms2.newNameSuffix);
			methodSt = methodSt.replace(" " + oldName, " " + funPart.name);
			String retSt;
			String returnClause = "return weilai345;";
			//
			if (funPart.retTypeSt.equals("void")) {
				retSt = "";
				returnClause = "";
			} else {
				retSt = funPart.retTypeSt.trim();
				if (retSt.isEmpty())
					Coms2.pause();
				if (retSt.charAt(0) == '<') {
					//eg. "<T> T ret=get(key);" ~> "T ret=get(key);"
					String st = Str9a.readNest(retSt, 0, '<', '>');
					retSt = retSt.substring(st.length()).trim();
				}
				retSt += " weilai345=";
			}
			String methodIdSt = "\"" + funPart.toString() + "\"";
			{
				String invokeOn = funPart.isStatic() ? funPart.clsPart.cname + ".class" : "this";
				String args = Misc9a.concatList(funPart.paramNames, ",");
				String cname = dent3 + HourTimes.Time9.class.getName();
				sb.append(cname + ".start(" + methodIdSt + ", " + invokeOn//
						+ (args.isEmpty() ? "" : ", " + args) + ");");

				sb.append(dent3 + retSt + funPart.name + "(" + args + ");");
				if (!returnClause.isEmpty()) {
					sb.append(cname + ".interceptValues(weilai345);");
					sb.append(dent3 + returnClause);
				}
			}
			sb.append(dent2 + "} finally {");
			sb.append(dent3 + HourTimes.Time9.class.getName() + ".stop(" + methodIdSt + ");");
			sb.append(dent2 + "}");
			sb.append(dent + "}\n");
			funPart.clsPart.file.addInsert(funPart.from, sb.toString(), 0);
			//
			funPart.setPrivateModifier();
			return true;
		}
	}

	public void printResult() {
	}

	Object abc() {
		return "";
	}

	public void start() {
		InsertCode0 app = new InsertTimeCostMethod_main();
		Coms2.srcBackupRoot = new File("c:/t/InsertTimeCostMethod_main");
		Coms2.file_copiedFilesList = new File(Coms2.srcBackupRoot, "CopiedFilesList");
		Coms2.getClassFromJava = new BiFunction<File, File, File[]>() {
			@Override
			public File[] apply(File root, File file) {
				//源根是src, src.dad()/target/classes是类根。
				File dad = root.getParentFile();
				while (!dad.getName().equals("src")) {
					dad = dad.getParentFile();
					if (dad == null) {
						return null;
					}
				}
				File cRoot = new File(dad.getParentFile(), "target/classes");
				if (cRoot.exists()) {
					String rela = File9a.relativePathOf(root, file);
					rela = rela.substring(0, rela.length() - 5) + ".class";
					File cFile = new File(cRoot, rela);
					return new File[] { cRoot, cFile };
				} else {
					return null;
				}
			}
		};
		int opt = 2;
		if (opt == 1) {
			Coms2.srcRoots = new File[] { new File("D:\\bearJ\\autoeq2\\src\\main\\src_eq3") };
			app.start_find_and_insert(new Predicate<File>() {
				@Override
				public boolean test(File file) {
					if (file.getPath().contains("")) {
						if (file.getName().matches(".+")) {
							return true;
						}
					}
					return false;
				}
			}, new BiFunction<File, String, String>() {
				@Override
				public String apply(File file, String method) {
					return "";
				}
			});
		} else if (opt == 2) {
			Coms2.srcRoots = JavaRoots.get_b1_tcli_roots();
			app.start_find_and_insert(new Predicate<File>() {//======================================
				@Override
				public boolean test(File file) {
					Ca.asert(file.isFile(), "");
					String st = file.getPath();
					String nam = file.getName();
					boolean isOkDir = st.contains("\\gate\\src\\main\\java\\")
							|| st.contains("\\java\\slds\\slds-client\\src\\");
					//
					//System.out.println("\t" + file.getAbsolutePath());
					boolean isOkFile = true;
					int opt = -1;
					if (opt == 1) {
						isOkDir = st.contains("\\tcli-service\\") || st.contains("\\infra-view\\")//
								|| st.contains("\\tcli-core\\");
						isOkFile = st.equals("JdbcAccessImpl.java") || st.equals("BoBase.java")
								|| st.equals("ViewServiceBase.java");
					}
					if (isOkDir) {
						if (isOkFile)
							return true;
					}
					return false;
				}
			}, new BiFunction<File, String, String>() {
				@Override
				public String apply(File file, String method) {
					if ("".isEmpty())
						return "";
					if (method.matches("")) {
						return "";
					}
					return null;
				}
			});
		}
		Coms2.log("end.");
	}

	public static void main(String[] args) {
		//insert into no patched file, and backup it before insertion.

		int opt = 1;
		opt = 2;
		Properties map = System.getProperties();
		System.out.println(map.get("java.class.path"));
		if (opt == 1) {
			findRourceDirs();
		} else if (opt == 2) {//b1 webclient.
			InsertTimeCostMethod_main main = new InsertTimeCostMethod_main();
			main.start();
		} else {
			File fil = new File(
					"C:\\Perforce\\b1a\\BUSMB_B1\\SBO\\dev\\Source\\WebClient\\java\\auth\\target\\classes\\com\\sap\\b1\\thin\\auth\\AuthLoginRequest.class");
		}
	}
}
